package com.example.springboot.mapper;

import com.example.springboot.domain.Comment;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;

import java.util.List;

@Mapper
@Component(value ="commentMapper")
public interface CommentMapper {
//    @Insert("insert into comment (content,userId,pid,rootId,time,fid) values (#{content},#{userId},#{pid},#{rootId},#{time},#{fid})")
    int insert(Comment comment);

    @Delete("delete from comment where id=#{id}")
    int deleteById(Integer id);

    @Update("update comment set content=#{comment.content},userId=#{comment.userId},pid=#{comment.pid},rootId=#{comment.rootId},time=#{comment.time},fid=#{comment.fid} where id=#{comment.id} ")
    void updateById(Comment comment,int id);

    @Select("select * from comment where id = #{id}")
    Comment selectById(Integer id);

    @Select("select comment.*, user.username as userName, user.avatar as avatar from comment left join user on comment.userId = user.userid " +
            "left join comment c2 on comment.pid = c2.id left join user u2 on c2.userId = u2.userid where comment.fid  = #{fid} and comment.rootId = #{rootId} order by id desc")
    List<Comment> selectAll(Comment comment);

    @Select("select comment.*, user.username as userName, user.avatar as avatar from comment left join user on comment.userId = user.userid where comment.fid  = #{fid} and pid is null order by id desc")
    List<Comment> selectForUser(Comment comment);

    @Select("select count(*) from comment where fid = #{fid}")
    Integer selectCount(@Param("fid") Integer fid);
}
